#include<iostream>
using namespace std;



int main()
{
	int T;
	cin>>T;
	
	while(T--)
	{
	int money;
	cin>>money;
    int rmb[7]={1,2,5,10,20,50,100}; 
    int c[7];
    for(int i=0;i<7;i++)
    {
		cin>>c[i];
	}
    
    int cnt=0;
    int r=money;
    for(int i=6;i>=0;i--)
    {
		if(r/rmb[i]>0&&c[i]>0)
		{
			if(r/rmb[i]<=c[i])
			{
				cnt+=r/rmb[i];
				r=r-( (r/rmb[i]) * rmb[i]); 
			}
			else 
			{
				cnt+=c[i];	
				r-=rmb[i]*c[i];
			}
		}
	}
	
	if(r==0){
		cout<<cnt<<endl;
		
	}
	else
	{
		cout<<"-1"<<endl;
		
	}
	}
    
	return 0;
} 
